home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / uucp / wcnews_1_0_30.lha / man / dbz3z.man < prev    next >
Text File  |  1994-01-04  |  16KB  |  397 lines

  1.  
  2.  
  3.  
  4. DBZ(3Z)                                                   DBZ(3Z)
  5.  
  6.  
  7. ›1mNAME›22m
  8.        dbminit,  fetch, store, dbmclose - somewhat dbm­compatible
  9.        database routines
  10.        dbzfresh, dbzagain, dbzfetch, dbzstore - database routines
  11.        dbzsync,   dbzsize,   dbzincore,   dbzcancel,  dbzdebug  -
  12.        database routines
  13.  
  14. ›1mSYNOPSIS›22m
  15.        ›1m#include›22m ›1m<dbz.h>›22m
  16.  
  17.        ›1mdbminit(base)›22m
  18.        ›1mchar›22m ›1m*base;›22m
  19.  
  20.        ›1mdatum›22m
  21.        ›1mfetch(key)›22m
  22.        ›1mdatum›22m ›1mkey;›22m
  23.  
  24.        ›1mstore(key,›22m ›1mvalue)›22m
  25.        ›1mdatum›22m ›1mkey;›22m
  26.        ›1mdatum›22m ›1mvalue;›22m
  27.  
  28.        ›1mdbmclose()›22m
  29.  
  30.        ›1mdbzfresh(base,›22m ›1msize,›22m ›1mfieldsep,›22m ›1mcmap,›22m ›1mtagmask)›22m
  31.        ›1mchar›22m ›1m*base;›22m
  32.        ›1mlong›22m ›1msize;›22m
  33.        ›1mint›22m ›1mfieldsep;›22m
  34.        ›1mint›22m ›1mcmap;›22m
  35.        ›1mlong›22m ›1mtagmask;›22m
  36.  
  37.        ›1mdbzagain(base,›22m ›1moldbase)›22m
  38.        ›1mchar›22m ›1m*base;›22m
  39.        ›1mchar›22m ›1m*oldbase;›22m
  40.  
  41.        ›1mdatum›22m
  42.        ›1mdbzfetch(key)›22m
  43.        ›1mdatum›22m ›1mkey;›22m
  44.  
  45.        ›1mdbzstore(key,›22m ›1mvalue)›22m
  46.        ›1mdatum›22m ›1mkey;›22m
  47.        ›1mdatum›22m ›1mvalue;›22m
  48.  
  49.        ›1mdbzsync()›22m
  50.  
  51.        ›1mlong›22m
  52.        ›1mdbzsize(nentries)›22m
  53.        ›1mlong›22m ›1mnentries;›22m
  54.  
  55.        ›1mdbzincore(newvalue)›22m
  56.  
  57.        ›1mdbzcancel()›22m
  58.  
  59.        ›1mdbzdebug(newvalue)›22m
  60.  
  61.  
  62.  
  63.  
  64.                             3 Feb 1991                          1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. DBZ(3Z)                                                   DBZ(3Z)
  71.  
  72.  
  73. ›1mDESCRIPTION›22m
  74.        These functions provide an indexing system for rapid  ran­
  75.        dom  access  to  a  text file (the ›4mbase›24m ›4mfile›24m).  Subject to
  76.        certain constraints, they are call­compatible with ›4mdbm›24m(3),
  77.        although  they  also  provide some extensions.  (Note that
  78.        they are ›4mnot›24m  file­compatible  with  ›4mdbm›24m  or  any  variant
  79.        thereof.)
  80.  
  81.        In  principle,  ›4mdbz›24m stores key­value pairs, where both key
  82.        and value are arbitrary sequences of bytes,  specified  to
  83.        the  functions  by  values of type ›4mdatum›24m, typedefed in the
  84.        header file to be a structure with members ›4mdptr›24m  (a  value
  85.        of  type  ›4mchar›24m ›4m*›24m pointing to the bytes) and ›4mdsize›24m (a value
  86.        of type ›4mint›24m indicating how long the byte sequence is).
  87.  
  88.        In practice, ›4mdbz›24m is  more  restricted  than  ›4mdbm›24m.   A  ›4mdbz›24m
  89.        database  must  be  an  index  into  a base file, with the
  90.        database ›4mvalue›24ms being ›4mfseek›24m(3) offsets into the base file.
  91.        Each such ›4mvalue›24m must ``point to'' a place in the base file
  92.        where the corresponding ›4mkey›24m sequence is found.  A key  can
  93.        be  no  longer  than  DBZMAXKEY (a constant defined in the
  94.        header file) bytes.  No key can be an initial  subsequence
  95.        of  another, which in most applications requires that keys
  96.        be either bracketed or terminated in  some  way  (see  the
  97.        discussion  of  the ›4mfieldsep›24m parameter of ›4mdbzfresh›24m, below,
  98.        for a fine point on terminators).
  99.  
  100.        ›4mDbminit›24m opens a database, an  index  into  the  base  file
  101.        ›4mbase›24m, consisting of files ›4mbase›1m›24m.dir›22m and ›4mbase›1m›24m.pag›22m which must
  102.        already exist.  (If the database is new,  they  should  be
  103.        zero­length   files.)   Subsequent  accesses  go  to  that
  104.        database until ›4mdbmclose›24m is called to close  the  database.
  105.        The  base  file need not exist at the time of the ›4mdbminit›24m,
  106.        but it must exist before accesses are attempted.
  107.  
  108.        ›4mFetch›24m searches the database for the specified ›4mkey›24m, return­
  109.        ing the corresponding ›4mvalue›24m if any.  ›4mStore›24m stores the ›4mkey›24m­
  110.        ›4mvalue›24m pair in the database.  ›4mStore›24m will  fail  unless  the
  111.        database  files  are writeable.  See below for a complica­
  112.        tion arising from case mapping.
  113.  
  114.        ›4mDbzfresh›24m is a  variant  of  ›4mdbminit›24m  for  creating  a  new
  115.        database  with  more  control  over  details.   Unlike for
  116.        ›4mdbminit›24m, the database files need not exist: they  will  be
  117.        created if necessary, and truncated in any case.
  118.  
  119.        ›4mDbzfresh›24m's  ›4msize›24m parameter specifies the size of the first
  120.        hash table within the database, in key­value pairs.   Per­
  121.        formance  will  be  best if ›4msize›24m is a prime number and the
  122.        number of key­value pairs stored in the database does  not
  123.        exceed  about  2/3  of ›4msize›24m.  (The ›4mdbzsize›24m function, given
  124.        the expected number of key­value  pairs,  will  suggest  a
  125.        database  size  that meets these criteria.)  Assuming that
  126.        an ›4mfseek›24m offset is 4 bytes, the ›1m.pag›22m file will  be  4*›4msize›24m
  127.  
  128.  
  129.  
  130.                             3 Feb 1991                          2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. DBZ(3Z)                                                   DBZ(3Z)
  137.  
  138.  
  139.        bytes (the ›1m.dir›22m file is tiny and roughly constant in size)
  140.        until the number of key­value pairs exceeds about  80%  of
  141.        ›4msize›24m.   (Nothing  awful  will happen if the database grows
  142.        beyond 100% of ›4msize›24m, but accesses will slow down  somewhat
  143.        and the ›1m.pag›22m file will grow somewhat.)
  144.  
  145.        ›4mDbzfresh›24m's  ›4mfieldsep›24m parameter specifies the field separa­
  146.        tor in the base file.  If this is not  NUL  (0),  and  the
  147.        last character of a ›4mkey›24m argument is NUL, that NUL compares
  148.        equal to either a NUL or a  ›4mfieldsep›24m  in  the  base  file.
  149.        This  permits  use of NUL to terminate key strings without
  150.        requiring that NULs appear in the base file.  The ›4mfieldsep›24m
  151.        of  a  database created with ›4mdbminit›24m is the horizontal­tab
  152.        character.
  153.  
  154.        For use in news systems, various  forms  of  case  mapping
  155.        (e.g.  uppercase to lowercase) in keys are available.  The
  156.        ›4mcmap›24m parameter to ›4mdbzfresh›24m is a single character  specify­
  157.        ing which of several mapping algorithms to use.  Available
  158.        algorithms are:
  159.  
  160.               ›1m0›22m      case­sensitive:  no case mapping
  161.  
  162.               ›1mB›22m      same as ›1m0›22m
  163.  
  164.               ›1mNUL›22m    same as ›1m0›22m
  165.  
  166.               ›1m=›22m      case­insensitive:  uppercase  and  lowercase
  167.                      equivalent
  168.  
  169.               ›1mb›22m      same as ›1m=›22m
  170.  
  171.               ›1mC›22m      RFC822   message­ID   rules,  case­sensitive
  172.                      before `@'  (with  certain  exceptions)  and
  173.                      case­insensitive after
  174.  
  175.               ›1m?›22m      whatever the local default is, normally ›1mC›22m
  176.  
  177.        Mapping algorithm ›1m0›22m (no mapping) is faster than the others
  178.        and is overwhelmingly the correct choice for most applica­
  179.        tions.   Unless compatibility constraints interfere, it is
  180.        more efficient to pre­map the keys, storing mapped keys in
  181.        the  base  file,  than to have ›4mdbz›24m do the mapping on every
  182.        search.
  183.  
  184.        For historical reasons, ›4mfetch›24m and ›4mstore›24m expect  their  ›4mkey›24m
  185.        arguments  to  be  pre­mapped, but expect unmapped keys in
  186.        the base file.  ›4mDbzfetch›24m and ›4mdbzstore›24m do the same jobs but
  187.        handle  all  case mapping internally, so the customer need
  188.        not worry about it.
  189.  
  190.        ›4mDbz›24m stores only the database ›4mvalue›24ms in its files,  relying
  191.        on  reference  to the base file to confirm a hit on a key.
  192.        References to the base  file  can  be  minimized,  greatly
  193.  
  194.  
  195.  
  196.                             3 Feb 1991                          3
  197.  
  198.  
  199.  
  200.  
  201.  
  202. DBZ(3Z)                                                   DBZ(3Z)
  203.  
  204.  
  205.        speeding up searches, if a little bit of information about
  206.        the keys can be stored in the ›4mdbz›24m files.  This is ``free''
  207.        if  there are some unused bits in an ›4mfseek›24m offset, so that
  208.        the offset can be ›4mtagged›24m with some information  about  the
  209.        key.   The ›4mtagmask›24m parameter of ›4mdbzfresh›24m allows specifying
  210.        the location of unused bits.  ›4mTagmask›24m  should  be  a  mask
  211.        with one group of contiguous ›1m1›22m bits.  The bits in the mask
  212.        should be unused (0) in ›4mmost›24m offsets.  The bit immediately
  213.        above  the mask (the ›4mflag›24m bit) should be unused (0) in ›4mall›24m
  214.        offsets; ›4m(dbz)store›24m will reject attempts to store  a  key­
  215.        value  pair in which the ›4mvalue›24m has the flag bit on.  Apart
  216.        from this restriction, tagging is invisible to  the  user.
  217.        As  a  special  case, a ›4mtagmask›24m of 1 means ``no tagging'',
  218.        for use with  enormous  base  files  or  on  systems  with
  219.        unusual offset representations.
  220.  
  221.        A ›4msize›24m of 0 given to ›4mdbzfresh›24m is synonymous with the local
  222.        default; the normal default  is  suitable  for  tables  of
  223.        90­100,000  key­value pairs.  A ›4mcmap›24m of 0 (NUL) is synony­
  224.        mous with the character  ›1m0›22m,  signifying  no  case  mapping
  225.        (note  that  the  character ›1m?›22m  specifies the local default
  226.        mapping, normally ›1mC›22m).  A ›4mtagmask›24m of 0 is  synonymous  with
  227.        the  local default tag mask, normally 0x7f000000 (specify­
  228.        ing the top bit in a 32­bit offset as the  flag  bit,  and
  229.        the  next  7  bits as the mask, which is suitable for base
  230.        files up to circa 24MB).  Calling ›4mdbminit(name)›24m  with  the
  231.        database    files   empty   is   equivalent   to   calling
  232.        ›4mdbzfresh(name,0,'\t','?',0)›24m.
  233.  
  234.        When databases are regenerated periodically, as  in  news,
  235.        it  is  simplest to pick the parameters for a new database
  236.        based on the old one.  This also permits  some  memory  of
  237.        past  sizes  of  the  old database, so that a new database
  238.        size can be chosen to cover expected fluctuations.   ›4mDbza­›24m
  239.        ›4mgain›24m  is  a variant of ›4mdbminit›24m for creating a new database
  240.        as a new generation of  an  old  database.   The  database
  241.        files  for  ›4moldbase›24m must exist.  ›4mDbzagain›24m is equivalent to
  242.        calling ›4mdbzfresh›24m with the same field separator, case  map­
  243.        ping,  and  tag mask as the old database, and a ›4msize›24m equal
  244.        to the result of applying ›4mdbzsize›24m to the largest number of
  245.        entries in the ›4moldbase›24m database and its previous 10 gener­
  246.        ations.
  247.  
  248.        When many accesses are being done by the same program, ›4mdbz›24m
  249.        is  massively faster if its first hash table is in memory.
  250.        If an internal flag is 1, an attempt is made to  read  the
  251.        table  in when the database is opened, and ›4mdbmclose›24m writes
  252.        it out to disk again (if it was read successfully and  has
  253.        been  modified).   ›4mDbzincore›24m  sets  the  flag  to ›4mnewvalue›24m
  254.        (which should be 0 or 1) and returns the  previous  value;
  255.        this  does  not  affect  the status of a database that has
  256.        already been opened.  The default is 0.   The  attempt  to
  257.        read the table in may fail due to memory shortage; in this
  258.        case ›4mdbz›24m quietly  falls  back  on  its  default  behavior.
  259.  
  260.  
  261.  
  262.                             3 Feb 1991                          4
  263.  
  264.  
  265.  
  266.  
  267.  
  268. DBZ(3Z)                                                   DBZ(3Z)
  269.  
  270.  
  271.        ›4mStore›24ms to an in­memory database are not (in general) writ­
  272.        ten out to the file  until  ›4mdbmclose›24m  or  ›4mdbzsync›24m,  so  if
  273.        robustness  in  the  presence  of  crashes  or  concurrent
  274.        accesses is crucial, in­memory databases  should  probably
  275.        be avoided.
  276.  
  277.        ›4mDbzsync›24m  causes  all buffers etc. to be flushed out to the
  278.        files.  It is  typically  used  as  a  precaution  against
  279.        crashes  or  concurrent  accesses when a ›4mdbz›24m­using process
  280.        will be running for a long time.  It is a somewhat  expen­
  281.        sive operation, especially for an in­memory database.
  282.  
  283.        ›4mDbzcancel›24m  cancels  any pending writes from buffers.  This
  284.        is typically useful  only  for  in­core  databases,  since
  285.        writes  are  otherwise done immediately.  Its main purpose
  286.        is to let a child process, in the wake of  a  ›4mfork›24m,  do  a
  287.        ›4mdbmclose›24m without writing its parent's data to disk.
  288.  
  289.        If  ›4mdbz›24m has been compiled with debugging facilities avail­
  290.        able (which makes it bigger and a  bit  slower),  ›4mdbzdebug›24m
  291.        alters  the  value  (and returns the previous value) of an
  292.        internal flag which (when 1; default is 0) causes  verbose
  293.        and cryptic debugging output on standard output.
  294.  
  295.        Concurrent  reading of databases is fairly safe, but there
  296.        is no (inter)locking, so concurrent updating is not.
  297.  
  298.        The database files include a record of the byte  order  of
  299.        the  processor creating the database, and accesses by pro­
  300.        cessors with different byte order will work, although they
  301.        will be slightly slower.  Byte order is preserved by ›4mdbza­›24m
  302.        ›4mgain›24m.  However, agreement on the size and internal  struc­
  303.        ture  of  an ›4mfseek›24m offset is necessary, as is consensus on
  304.        the character set.
  305.  
  306.        An open database occupies three ›4mstdio›24m  streams  and  their
  307.        corresponding  file descriptors; a fourth is needed for an
  308.        in­memory  database.   Memory  consumption  is  negligible
  309.        (except for ›4mstdio›24m buffers) except for in­memory databases.
  310.  
  311. ›1mSEE›22m ›1mALSO›22m
  312.        dbz(1), dbm(3)
  313.  
  314. ›1mDIAGNOSTICS›22m
  315.        Functions returning ›4mint›24m values return 0  for  success,  -1
  316.        for  failure.   Functions  returning ›4mdatum›24m values return a
  317.        value with ›4mdptr›24m set to NULL for failure.  ›4mDbminit›24m attempts
  318.        to  have ›4merrno›24m set plausibly on return, but otherwise this
  319.        is not guaranteed.  An ›4merrno›24m of ›1mEDOM›22m  from  ›4mdbminit›24m  indi­
  320.        cates  that  the database did not appear to be in ›4mdbz›24m for­
  321.        mat.
  322.  
  323. ›1mHISTORY›22m
  324.        The original  ›4mdbz›24m  was  written  by  Jon  Zeeff  (zeeff@b­
  325.  
  326.  
  327.  
  328.                             3 Feb 1991                          5
  329.  
  330.  
  331.  
  332.  
  333.  
  334. DBZ(3Z)                                                   DBZ(3Z)
  335.  
  336.  
  337.        tech.ann­arbor.mi.us).   Later contributions by David But­
  338.        ler and Mark Moraes.  Extensive reworking, including  this
  339.        documentation, by Henry Spencer (henry@zoo.toronto.edu) as
  340.        part of the C News project.   Hashing  function  by  Peter
  341.        Honeyman.
  342.  
  343. ›1mBUGS›22m
  344.        The  ›4mdptr›24m members of returned ›4mdatum›24m values point to static
  345.        storage which is overwritten by later calls.
  346.  
  347.        Unlike ›4mdbm›24m, ›4mdbz›24m will misbehave if  an  existing  key­value
  348.        pair  is  `overwritten'  by a new ›4m(dbz)store›24m with the same
  349.        key.  The user is responsible for avoiding this  by  using
  350.        ›4m(dbz)fetch›24m  first  to  check  for  duplicates; an internal
  351.        optimization remembers the result of the first  search  so
  352.        there is minimal overhead in this.
  353.  
  354.        Waiting  until  after  ›4mdbminit›24m to bring the base file into
  355.        existence will fail if ›4mchdir›24m(2) has been used meanwhile.
  356.  
  357.        The RFC822 case mapper implements only a first  approxima­
  358.        tion to the hideously­complex RFC822 case rules.
  359.  
  360.        The prime finder in ›4mdbzsize›24m is not particularly quick.
  361.  
  362.        Should  implement  the ›4mdbm›24m functions ›4mdelete›24m, ›4mfirstkey›24m, and
  363.        ›4mnextkey›24m.
  364.  
  365.        On C implementations which trap integer overflow, ›4mdbz›24m will
  366.        refuse to ›4m(dbz)store›24m an ›4mfseek›24m offset equal to the greatest
  367.        representable positive number, as this would  cause  over­
  368.        flow in the biased representation used.
  369.  
  370.        ›4mDbzagain›24m  perhaps ought to notice when many offsets in the
  371.        old database were too big for tagging, and shrink the  tag
  372.        mask to match.
  373.  
  374.        Marking  ›4mdbz›24m's  file  descriptors close­on­›4mexec›24m would be a
  375.        better approach to the problem ›4mdbzcancel›24m tries to address,
  376.        but that's harder to do portably.
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.                             3 Feb 1991                          6
  395.  
  396.  
  397.